package com.handpay.zztong.hp.login.model.sendphonecode;

import android.text.TextUtils;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.SecureManager;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.HttpUrlConstant;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.util.Hashtable;

/**
 * Created by lyang on 2017/3/16.
 */

public class SendAuthCodeModel extends BaseModel {
    private ZZTongActivity mActivity;
    private BusinessEngineListener mBusinessEngineListener ;

    public void doSendAuthCodePost(ZZTongActivity activity, String action, String phoneNumber, BusinessEngineListener listener) {
        mBusinessEngineListener = listener;
        mActivity = activity;
        Hashtable<String, String> paramers = new Hashtable<String, String>();
        String account;
        if (TextUtils.isEmpty(phoneNumber)) {
            account = AccountUtils.getAccountPhone(activity);
        } else {
            account = phoneNumber;
        }
        if (!TextUtils.isEmpty(account)) {
            paramers.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(account, 1, null));
            paramers.put("action", action);
            paramers.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
            doPost(activity, HttpUrlConstant.Do_SendPhoneCode, paramers, true, false);
        }
    }

    @Override
    public boolean netResponse(String action, byte[] data) {
        boolean bResult = super.netResponse(action, data);
        if (bResult) {
            return true;
        }
        if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
            ResponseBean respBean = null;
            try {
                String result = new String(data, "UTF-8");
                respBean = FastJsonHelper.getObject(result, ResponseBean.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (mBusinessEngineListener != null) {
                mBusinessEngineListener.callbackBusinessResponse(respBean);
            }
        } else {

        }
        return true;
    }
}
